﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using Jayrock.Json;
using tools;

/// <summary>
/// Summary description for SdfEditor
/// </summary>
[WebService(Namespace = "http://localhost/sdf/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SdfEditor : WebService
{
    [WebMethod]
    public bool AddNewConstraint(string fileName, string propertyName, string constraint)
    {
        var success = false;
        constraint = HttpUtility.UrlDecode(constraint);
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new SchemaEditor(pathToFile);

        var properties = editor.GetSchemaProperties().ToList();
        foreach (var p in properties.Where(x => x.Name == propertyName))
        {
            if (!p.ValueConstraintsList.Contains(constraint))
            {
                p.AddConstraint(constraint);
                editor.ApplySchema(properties.ToArray());
                success = true;
            }
            break;
        }
        return success;
    }
    [WebMethod]
    public bool DeleteConstraint(string fileName, string propertyName, string constraint)
    {
        var success = false;
        constraint = HttpUtility.UrlDecode(constraint);

        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new SchemaEditor(pathToFile);
        var properties = editor.GetSchemaProperties().ToList();
        foreach (var p in properties.Where(x => x.Name == propertyName))
        {
            if (p.ValueConstraintsList.Contains(constraint))
            {
                p.DeleteConstraint(p.ValueConstraintsList.IndexOf(constraint));
                editor.ApplySchema(properties.ToArray());
                success = true;
            }
            else if (p.ValueConstraintsList.Contains(String.Format("'{0}'", constraint)))
            {
                p.DeleteConstraint(p.ValueConstraintsList.IndexOf(String.Format("'{0}'", constraint)));
                editor.ApplySchema(properties.ToArray());
                success = true;
            }
            break;
        }
        return success;
    }
    [WebMethod]
    public string[] GetConstraints(string fileName, string propertyName)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new SchemaEditor(pathToFile);
        /*
        NonFeatureEditor _ed = new NonFeatureEditor(_pathToFile);
        
        int r1 = _ed.SetDefaultValueToNulls("SUL_TEMP", "STATUS", "ללא");
        int r11 = _ed.SetValue("SUL_TEMP", "STATUS=''", "STATUS", "ללא");
        */
        var _properties = editor.GetSchemaProperties().ToList();
        foreach (var p in _properties.Where(x => x.Name == propertyName))
        {
            if (p.ValueConstraintsList != null && p.ValueConstraintsList.Count != 0)
            {
                return p.ValueConstraintsList.ToArray();
            }
            break;
        }
        return null;
    }
    [WebMethod]
    public int NfcAddNewEntry(string fileName, string className, string entry)
    {
        var _pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new NonFeatureEditor(_pathToFile);
        return editor.AddNewEntry(className, HttpUtility.UrlDecode(entry));
    }
    [WebMethod]
    public bool NfcRenameEntry(string fileName, string className, int id, string newValue)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new NonFeatureEditor(pathToFile);
        return editor.RenameEntry(className, id, HttpUtility.UrlDecode(newValue));
    }
    [WebMethod]
    public bool NfcDeleteEntryByName(string fileName, string className, string entry)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new NonFeatureEditor(pathToFile);
        return editor.DeleteEntry(className, HttpUtility.UrlDecode(entry));
    }
    [WebMethod]
    public bool NfcDeleteEntryById(string fileName, string className, int id)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));

        var editor = new NonFeatureEditor(pathToFile);
        return editor.DeleteEntry(className, id);
    }
    [WebMethod]
    public string NfcGetAllEntries(string fileName, string className)
    {
        return NfcGetAllEntriesWithCode(fileName, className, String.Empty);
    }
    [WebMethod]
    public string NfcGetAllEntriesWithCode(string fileName, string className, string alternateKeyName)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));
        var editor = new NonFeatureEditor(pathToFile);
        var entries = editor.GetAllEntries(className, alternateKeyName);
        var arr = new JsonArray(entries);
        return arr.ToString();
    }
    [WebMethod]
    public string NfcAllEntriesToXml(string fileName, string className)
    {
        return NfcAllEntriesToXmlWithCode(fileName, className, String.Empty);
    }
    [WebMethod]
    public string NfcAllEntriesToXmlWithCode(string fileName, string className, string alternateKeyName)
    {
        var pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], HttpUtility.UrlDecode(fileName));
        var editor = new NonFeatureEditor(pathToFile);
        var entries = editor.GetAllEntries(className, alternateKeyName);

        var doc = new XmlDocument();
        var root = doc.CreateElement(className);

        foreach (var entry in entries)
        {
            var node = doc.CreateElement("entry");
            var keyNode = doc.CreateElement("key");
            keyNode.InnerText = entry.Key.ToString();
            var valueNode = doc.CreateElement("value");
            valueNode.InnerText = entry.Value;
            node.AppendChild(keyNode);
            node.AppendChild(valueNode);
            root.AppendChild(node);
        }
        doc.AppendChild(root);
        return doc.InnerXml;
    }
}

